package com.example.triprec.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.triprec.R;
import com.example.triprec.entity.Collect;
import com.google.android.material.imageview.ShapeableImageView;

import java.util.List;

public class MyCollectionAdapter extends RecyclerView.Adapter<MyCollectionAdapter.ViewHolder> {

    private List<Collect> collectList;
    private OnItemClickListener onItemClickListener;

    public MyCollectionAdapter(List<Collect> collectList) {
        this.collectList = collectList;
    }

    public interface OnItemClickListener {
        void onCancelClick(Collect collect, int position);

        void onViewDetailClick(Collect collect);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_collection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Collect collect = collectList.get(position);

        // 设置景点名称
        holder.tvScenicName.setText(collect.getScenic().getName());

        // 设置景点位置
        holder.tvScenicLocation.setText(collect.getScenic().getLocation());

        // 设置景点图片
        if (collect.getScenic().getImageResources() != null && collect.getScenic().getImageResources().length > 0) {
            holder.ivScenicImage.setImageResource(collect.getScenic().getImageResources()[0]);
        }

        // 设置收藏时间
        holder.tvCollectTime.setText("收藏于：" + collect.getCreateTime());

        // 设置点击事件
        holder.tvViewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onViewDetailClick(collect);
                }
            }
        });

        holder.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onCancelClick(collect, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return collectList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ShapeableImageView ivScenicImage;
        TextView tvScenicName;
        TextView tvScenicLocation;
        TextView tvCollectTime;
        TextView tvViewDetail;
        TextView tvCancel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivScenicImage = itemView.findViewById(R.id.iv_scenic_image);
            tvScenicName = itemView.findViewById(R.id.tv_scenic_name);
            tvScenicLocation = itemView.findViewById(R.id.tv_scenic_location);
            tvCollectTime = itemView.findViewById(R.id.tv_collect_time);
            tvViewDetail = itemView.findViewById(R.id.tv_view_detail);
            tvCancel = itemView.findViewById(R.id.tv_cancel);
        }
    }
} 